package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.view.ui.activity.transport.OfflineCollectionConfirmActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/7/6
 */
public class OfflineCollectionConfirmModel extends AndroidViewModel {

    public MutableLiveData<String> uploadImgLD = new MutableLiveData<>();
    public MutableLiveData<Boolean> sureCollectionLD = new MutableLiveData<>();

    public OfflineCollectionConfirmModel(@NonNull @NotNull Application application) {
        super(application);
    }

    //上传多图片
    public void uploadMultiFileModel(OfflineCollectionConfirmActivity activity, File file) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + "image.jpg\"", requestBody);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || TextUtils.isEmpty(response.body().getState())) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showLongToast(activity, "上传失败");
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().toString();
                    if (!TextUtils.isEmpty(url)) {
                        uploadImgLD.postValue(url);
                    } else {
                        DialogHelper.dismissProgressDialog();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    //确认收款
    public void sureCollection(String no, String images) {
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", no);
        map.put("images", images);
        NewAPIManager.getInstance().getLogisticsAPI().offlineUploadCredentials(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data != null && data.getState().equals(APIManager.RESPONSE_OK)) {
                    sureCollectionLD.postValue(true);
                }
            }
        });
    }
}
